﻿
using System.Linq;
using Common;
using MvcDesign.Core.CoreViewModel;

namespace MvcDesign.Core.CoreServices
{
    class UserViewService : IUserViewService
    {
        public UserProfile GetUserProfile(long userId)
        {
            throw new System.NotImplementedException();
        }

        private User ToUserModel(CoreDataModel.User user)
        {
            if (user == null || !user.Id.HasValue)
            {
                return null;
            }

            var model = new User
            {
                Id = user.Id.Value,
                IsModerator = false,
                Name = user.DisplayName,
                Url = MvcHelper.UrlHelper.RouteUrl(new { application = Constants.Applications.Profile.Name, action = Constants.Applications.Profile.Actions.User, id = user.Id })
            };

            return model;
        }

        public User GetUser(long userId)
        {
            using (var domainModel = new DataAccess.DataContext())
            {
                return ToUserModel(domainModel.GetCollection<CoreDataModel.User>().Find(userId));
            }
        }

        public User GetUser(string userName)
        {
            using (var domainModel = new DataAccess.DataContext())
            {
                return ToUserModel(domainModel.GetCollection<CoreDataModel.User>().SingleOrDefault(u => u.Name == userName));
            }
        }
    }
}
